<?php
namespace app\index\model;
use think\Db;
/**
 * Created by Submint Text.
 * User: hetaoren<452649510@qq.com>
 * Date: 2017/03/10
 * Explain: 库存类
 */
class Stocks extends Base{
     /**
      *  库存列表
      */
	public function pageQuery(){
		$areaIdPath = input('areaIdPath');
		$shopName = input('shopName');
		$goodsName = input('goodsName');
		$where = [];
		$where['s.dataFlag'] = 1;
		if($areaIdPath !='')$where['h.areaIdPath'] = ['like',$areaIdPath."%"];
		if($shopName != ''){
			$where['h.shopName'] = ['like',"%$shopName%"];
		}
		if($goodsName != ''){
			$where['g.goodsName'] = ['like',"%$goodsName%"];
		}
		if(session("HTR_STAFF.staffRoleId") == 2)$where['s.shopId'] = session("HTR_STAFF.shopId");	
		$page = Db::name('stocks')->alias('s')->join('__GOODS__ g','g.goodsId=s.goodsId','left')
				->join('__SHOPS__ h','h.shopId=s.shopId','left')
		        ->where($where)->field('g.*,h.shopName,h.areaIdPath,s.*')
		        ->order('stockId desc')->paginate(input('pagesize/d'))->toArray();
		if(count($page['Rows'])>0){
			foreach ($page['Rows'] as $key => $v){
  				$page['Rows'][$key]['profit'] = $v['salePrice']-$v['costPrice'];
  				$page['Rows'][$key]['areaPathName'] = $this->areaPathName($v['areaIdPath']);
  				$page['Rows'][$key]['goodsStock'] = $v['goodsStock'] <= $v['cautionNum'] ? '<span style="color:#f00">'.$v['goodsStock'].'</span>' : $v['goodsStock'];
			}
		}
		return $page;
	}

	public function areaPathName($areaIdPath){
		$areaIdPath = explode('_',$areaIdPath);
		$areaPathName = '';
		foreach($areaIdPath as $v){
			if($v)$areaPathName .= ' '.model('Areas')->getNames($v);
		}
		return $areaPathName;
	}
	/**
	 * 新增
	 */
	public function add(){
		$data = input('post.');
		$data["createTime"] = date('Y-m-d H:i:s');
		if(!$data["shopId"])return HTRReturn('请选择店铺');
		if(!$data["goodsId"])return HTRReturn('请选择商品');
        $count = $this->where(['goodsId'=>$data["goodsId"],'shopId'=>$data["shopId"],'dataFlag'=>1])->count();
        if($count)return HTRReturn('该商品库存已添加');
		$result = $this->allowField(true)->save($data);
        if(false !== $result){
        	return HTRReturn("新增成功", 1);
        }else{
        	return HTRReturn($this->getError());
        }
	}
    /**
	 * 编辑
	 */
	public function edit(){
		$stockId = input('post.stockId/d',0);
		$data = input('post.');
		HTRUnset($data, 'shopId,goodsId,stockId,createTime');
		$result = $this->allowField(true)->save($data,['stockId'=>$stockId]);
        if(false !== $result){
        	return HTRReturn("编辑成功", 1);
        }else{
        	return HTRReturn($this->getError());
        }
	}
	public function upStock(){		
		$goodsId = input('goodsId/d',0);
		$shopId = input('shopId/d',0);
		$type = input('type/d',0);
		$num = input('num/d',0);
		$desc = input('desc','');
		if($num < 1)return HTRReturn("数量不正确");
		if($desc == '')return HTRReturn("描述不能为空");
        return model('stockLog')->add($goodsId,$shopId,$type,$num,$desc);
	}

	/**
	 * 删除库存
	 */
	public function del(){
	    $id = input('post.id/d');
		$data = [];
		$data['dataFlag'] = -1;
	    $result = $this->update($data,['stockId'=>$id]);
        if(false !== $result){
        	return HTRReturn("删除成功", 1);
        }
        return HTRReturn('删除失败');
	}
}
